﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace McDonalds_v1._0
{
    public partial class multimedia_upload : System.Web.UI.Page
    {
        McDonaldsDataContext db = new McDonaldsDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

            var mediadata = from media in db.MEDIAs select media;

            string[] datakeynames = new string[1];
            datakeynames[0] = "MEDIANR";
            GridView1.DataKeyNames = datakeynames;
            GridView1.DataSource = mediadata;
            GridView1.DataBind();


            if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
            {
                string sFileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
                Label1.Text = sFileName;
                FileUpload1.PostedFile.SaveAs("C:\\Users\\Ramon\\documents\\visual studio 2010\\Projects\\McDonalds_v1.0\\McDonalds_v1.0\\images\\" + sFileName);

                MEDIA media = new MEDIA()
                {                        
                       LINK = sFileName,              
                       SIZE = FileUpload1.PostedFile.ContentLength,             
                       DATE = DateTime.Now,               
                       TYPE = FileUpload1.PostedFile.ContentType
                };

                db.MEDIAs.InsertOnSubmit(media);
                db.SubmitChanges();
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int medianr = Int32.Parse(GridView1.SelectedDataKey.Value.ToString());
            var selectedMedia = from media in db.MEDIAs where media.MEDIANR == medianr select media.LINK;
            
            Response.Redirect("images/" + selectedMedia.First());
        }


    }
}